package gsfordservlet.web;

import java.util.Date;

import gsfordservlet.domain.GsfordEvent;
import gsfordservlet.service.GsfordEventManager;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *  
 * @author Kyungbaek Kim <kyungbaekkim@gmail.com>
 * @version 2012.0202 (YYYY.MMDD)
 * @since 1.6 (Java Version)
 */
public class InitNotificationFormController extends SimpleFormController {
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());
    
    private GsfordEventManager eventManager;

    public ModelAndView onSubmit(Object command) throws ServletException {
    	GsfordEvent ge = (GsfordEvent)command;
    	
    	ge.setDate(new Date().toString());
    	
    	logger.info("getting GsfordEvent "+ ge.toString() );
    	
    	// store and send event
    	eventManager.addGsfordEvent(ge);

    	logger.info("returning to " + getSuccessView());

        return new ModelAndView(new RedirectView(getSuccessView()));
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    	GsfordEvent ge = new GsfordEvent();   
        
        return ge;
    }
        
    public void setEventManager(GsfordEventManager gem){
    	this.eventManager = gem;
    }
    
    public GsfordEventManager getEventManager(){
    	return this.eventManager;
    }
}
